package com.frank.loadinglibrary.special.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import com.frank.loadinglibrary.special.dot.Config;
import com.frank.loadinglibrary.special.dot.anim.LoopCircleAnimator;
import com.frank.loadinglibrary.special.dot.anim.SmallAndDropAnimator;
import com.frank.loadinglibrary.special.dot.anim.TextAnimator;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

import java.util.ArrayList;

/**
 * @author SUQI
 * @date 2021/6/3
 * @description
 **/
public class DotLoadingElement extends BaseElement implements BaseAnimationController.IAnimationController<AnimatorGroup> {

    private Paint mPaint;
    private LoopCircleAnimator mLoopCircleAnim;
    private SmallAndDropAnimator mSapAnim;
    private TextAnimator mTextAnim;
    private boolean isInit = false;

    private DotLoadingElement(IComponentCallback iComponentCallback) {
        this.callback = iComponentCallback;
    }

    @Override
    public ArrayList<AnimatorGroup> onCreateAnimators() {

        if (mLoopCircleAnim == null) {
            return null;
        }
        ArrayList<AnimatorGroup> list = new ArrayList<>();
        AnimatorGroup mAnimSet = new AnimatorGroup();
        // 顺序播放
        mAnimSet.runSerially(mLoopCircleAnim, mSapAnim, mTextAnim);
        mAnimSet.setLoopedCount(Animator.INFINITE);
        list.add(mAnimSet);
        return list;
    }

    @Override
    public void onAnimationReset() {
        //do nothing because donot need to reset animation params
    }

    @Override
    public BaseAnimationController.IAnimationController<AnimatorGroup> initAnimationController() {
        return this;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(4);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (isInit) {
            mLoopCircleAnim.draw(canvas, mPaint);
            mSapAnim.draw(canvas, mPaint);
            mTextAnim.draw(canvas, mPaint);
        } else {
            isInit = true;
            initConfig();
            apply();
            startAnimators();
        }

    }

    public IComponentCallback getCallback() {
        return callback;
    }

    private void apply() {
        mLoopCircleAnim = new LoopCircleAnimator(this);
        mSapAnim = new SmallAndDropAnimator(this, mLoopCircleAnim.getWolf());
        mTextAnim = new TextAnimator(this);
    }

    public void initConfig() {
        Config.TEXT_SIZE = (int) (getWidth() / (TextAnimator.STR.length() - 2) / 2);
        float broad = Math.min(getWidth(), getHeight());
        Config.CENTER_X = getWidth() / 2f;
        Config.CENTER_Y = getHeight() / 2f;
        Config.START_X = getWidth() / 2f;
        Config.START_Y = getHeight() / 2f - (broad / 3) + (Config.TEXT_SIZE + 30) / 2f;
        Config.BIG_CIRCLE_RADIUS = getHeight() / 2f - Config.START_Y;
    }

    public static class DotLoadingBuilder {
        private IComponentCallback iComponentCallback;

        public DotLoadingBuilder(IComponentCallback iComponentCallback) {
            this.iComponentCallback = iComponentCallback;
        }

        public DotLoadingElement build() {
            DotLoadingElement dotLoadingElement = new DotLoadingElement(iComponentCallback);

            return dotLoadingElement;
        }
    }
}
